from django.db import models


class MyModel(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()

    def __str__(self):
        return self.name


class board(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=100)

    class Meta:
        db_table = 'board'


class Author(models.Model):
    name = models.CharField(max_length=100, verbose_name='作者姓名')
    age = models.IntegerField(null=True, blank=True, verbose_name='年龄')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '作者'
        verbose_name_plural = '作者表'
        db_table = 'Author'


class Book(models.Model):
    title = models.CharField(max_length=100, verbose_name='标题')
    authors = models.ManyToManyField('Author', related_name='authored_books', verbose_name='作者')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '图书'
        verbose_name_plural = '图书表'
        db_table = 'book'


class Author_Book(models.Model):  # 通常不需要单独为ManyToManyField创建中间表，除非有额外字段
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='author_book_set', verbose_name='作者')
    book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='book_author_set', verbose_name='图书')

    def __str__(self):
        return f'{self.author} & {self.book}'

    class Meta:
        verbose_name = '作者图书关联'
        verbose_name_plural = '作者图书关联表'
        db_table = "Author_book"


class UserType(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name='名称')
    class Meta:
        verbose_name = '用户类型表'
        db_table = "user_type"


class User(models.Model):
    name = models.CharField(max_length=50, verbose_name='姓名')
    email = models.CharField(max_length=50, unique=True, verbose_name='邮箱')
    user_type = models.ForeignKey(UserType, null=True, on_delete=models.SET_NULL, verbose_name='用户类型')

    class Meta:
        verbose_name = '用户表'
        db_table = "user"
